package com.advert.boot.system.controller;

import cn.idev.excel.EasyExcel;
import cn.idev.excel.ExcelWriter;
import cn.idev.excel.write.metadata.WriteSheet;
import com.advert.boot.common.result.PageResult;
import com.advert.boot.common.result.Result;
import com.advert.boot.system.model.query.ProductPageQuery;
import com.advert.boot.system.model.vo.ProductDetailsVO;
import com.advert.boot.system.model.vo.ProductVO;
import com.advert.boot.system.service.AdvertProductService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 产品信息前端控制层
 *
 * @author Theo
 * @since 2024-07-30 11:25
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Tag(name = "14.客户端-产品信息")
@RequestMapping("/api/client/product")
public class AdvertClientController {
    private final AdvertProductService productService;

    @Operation(summary = "首页")
    @GetMapping("/homePage")
    public PageResult<ProductVO> homePage(@ParameterObject ProductPageQuery pageQuery) {
        return PageResult.success(productService.page(pageQuery, 1));
    }

    @Operation(summary = "查询详情")
    @GetMapping("/{id}/form")
    public Result<ProductDetailsVO> getConfigForm(@Parameter(description = "产品信息ID") @PathVariable Long id) {
        return Result.success(productService.selectProductById(id));
    }


    public void exportExcel3(HttpServletResponse response) throws IOException {
        setExportHeader(response);
        try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), ProductVO.class).build()) {
            // 获取总的数量
            Long count =  1000L/*employeeMapper.selectCount(null)*/;
            Integer pages = 20;
            Long size = count / pages;

            for (int i = 0; i < pages; i++) {
                WriteSheet writeSheet = EasyExcel.writerSheet(i, "模板" + i).build();

                Page<ProductVO> page = new Page<>();
                page.setCurrent(i + 1);
                page.setSize(size);
                Page<ProductVO> selectPage = null;//employeeMapper.selectPage(page, null);

                excelWriter.write(selectPage.getRecords(), writeSheet);
            }
        }
    }

    private static void setExportHeader(HttpServletResponse response) {
//        response.setContentType(CONTENT_TYPE);
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + "test.xlsx");
    }


}
